package com.bio.chatroom.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 用于与客户端交互的线程处理类
 */
public class ChatHandler implements Runnable{

    private ChatServer chatServer;
    private Socket socket;

    public ChatHandler(ChatServer chatServer, Socket socket) {
        this.chatServer = chatServer;
        this.socket = socket;
    }


    @Override
    public void run() {
        try {
            //存储新上线用户
            chatServer.addClient(socket);

            //读取用户发送消息
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(socket.getInputStream())
            );

            String msg = null;
            while ((msg = reader.readLine()) !=null) {
                String forwardMessage = "客户端["+ socket.getPort() +"]：" + msg + "\n";
                System.out.println(forwardMessage);
                //将消息转发给聊天室里在线的其他用户
                chatServer.forwardMessage(socket, forwardMessage);
                //检查用户是否准备退出
                if (chatServer.readyToQuit(msg)) {
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                chatServer.removeClient(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }






















}
